package com.msite.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Extra {

	public static List<Integer> strToListBy(String str) {
		List<Integer> list = new ArrayList<Integer>();
		if(str != null && str.length() > 0){
			String[] strArry = str.split(",");
			for (int i = 0; i < strArry.length; i++) {
				list.add(Integer.parseInt(strArry[i]));
			}
		}
		return list;
	}

	/**
	 * 获取航班退改签信息
	 *//*
	public static ModifyAndRefundStipulateVo getSOAPStipulate(String airlineCode, String classCode,
							  String depCode, String depDate, String arrCode){
		GetModifyAndRefundStipulateServiceImpl10Service
				serviceImpl10Service = new GetModifyAndRefundStipulateServiceImpl10Service();
		GetModifyAndRefundStipulateRequest stipulateRequest = new GetModifyAndRefundStipulateRequest();
		stipulateRequest.setAgencyCode(ConstantDomain.WY_AGENCY_CODE_ZHENGSHI);
		stipulateRequest.setSign(Md5Encrypt.md5(
				ConstantDomain.WY_AGENCY_CODE_ZHENGSHI
						+ airlineCode + arrCode
						+ classCode
						+ depCode + depDate
						+ ConstantDomain.WY_SAFETY_CODE_ZHENGSHI
		));
		stipulateRequest.setAirlineCode(airlineCode);
		stipulateRequest.setClassCode(classCode);
		stipulateRequest.setDepCode(depCode);
		stipulateRequest.setDepDate(depDate);
		stipulateRequest.setArrCode(arrCode);
		GetModifyAndRefundStipulateReply reply = serviceImpl10Service
				.getGetModifyAndRefundStipulateServiceImpl10Port()
				.getModifyAndRefundStipulate(stipulateRequest);
		ModifyAndRefundStipulateVo v = null;
		if("S".equals(reply.getReturnCode())){
			v = reply.getModifyAndRefundStipulateList();
			try {
				v.setChangeStipulate(URLDecoder.decode(v.getChangeStipulate(), "utf-8"));
				v.setModifyStipulate(URLDecoder.decode(v.getModifyStipulate(), "utf-8"));
				v.setRefundStipulate(URLDecoder.decode(v.getRefundStipulate(), "utf-8"));
				return v;
			} catch (UnsupportedEncodingException e) {
			}
		}
		return v;
	}*/

	public static String timeDifference(String depDate, String depTime, String arrTime){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
			Date depStart = sdf.parse(depDate + " " + depTime);
			Date arrEnd = sdf.parse(depDate + " " + arrTime);
			Calendar c = Calendar.getInstance();
			c.setTime(arrEnd);
			c.add(Calendar.DAY_OF_MONTH, 1);
			long t = c.getTime().getTime() - depStart.getTime();
			t = t > 24 * 3600000 ? t - 24*3600000 : t;
			int h = (int)(t / 3600000);
			int m = (int)((t - h * 3600000)/60000);
			return h + "时" + m + "分";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 清除中文及中文符号， 含空格符
	 * @param str
	 * @return
	 */
	public static String clearChar(String str){
		if(str == null){
			return str;
		}
		return str.replaceAll("[^x00-xff]*", "");
	}

	/**
	 * 计算两个日期之间相差的天数
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 */
	public static int daysBetween(Date bdate, Date smdate){
		long between_days= 0;
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(smdate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(bdate);
			long time2 = cal.getTimeInMillis();
			between_days = (time2-time1)/(1000*3600*24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new BigDecimal(between_days).intValue();
	}

	public static int daysBetween(String bdate, Date smdate){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date depDate = sdf.parse(bdate);
			return daysBetween(depDate, smdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return BigDecimal.ZERO.intValue();
	}

	public static void main(String[] args) {
		//Extra.getSOAPStipulate("MU", "U", "SHA", "2015-11-04", "PEK");
		//String r = Extra.timeDifference("2015-11-04", "1420", "1455");
		//System.out.println(r);
		/*String str = "CA320(dfd)（水电费）";
		str =  str.replaceAll("[^x00-xff]*", "");
		System.out.println(str+"-");*/
		int r  = Extra.daysBetween("2016-2-7", new Date());
		System.out.println(r);
	}
}
